package com.yjf.ljr.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

import com.yjf.ljr.application.MyApplication;
import com.yjf.ljr.util.SPreferences;

public class LockPatternService extends Service{

	public ScreenBroadcastReceiver mScreenReceiver;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		registerScreenReceiver();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		unRegisterScreenReceiver();
		//当上锁功能失效时，同时取消自动登陆功能
		((MyApplication)getApplication()).stopAutoLogin();
	}
	
	public void registerScreenReceiver() {
		if(mScreenReceiver == null) {
			mScreenReceiver = new ScreenBroadcastReceiver();
		}
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		registerReceiver(mScreenReceiver, filter);
	}
	
	public void unRegisterScreenReceiver() {
		unregisterReceiver(mScreenReceiver);
	}
	
	/**
	 * screen状态广播接收者
	 */
	private class ScreenBroadcastReceiver extends BroadcastReceiver {
		private String action = null;

		@Override
		public void onReceive(Context context, Intent intent) {
			action = intent.getAction();
			if (Intent.ACTION_SCREEN_ON.equals(action)) {
				
			} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
				SPreferences.saveData(LockPatternService.this, false, "ISFOREGROUND_KEY");
			}
		}
	}
	
}
